package org.odefu.model.project;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class TableTemplate implements Named
{
	private final List<TableColumn> columns = new ArrayList<TableColumn>();

	private final String name;

	public TableTemplate(final String name)
	{
		this.name = name;
	}

	@Override
	public String name()
	{
		return name;
	}

	public List<TableColumn> columns()
	{
		return Collections.unmodifiableList(columns);
	}

	public TableColumn column(final int index)
	{
		return columns.get(index);
	}

	public void addColumn(final TableColumn column)
	{
		if (!columns.contains(column))
		{
			columns.add(column);
		}
	}

	public void addColumn(final TableColumn column, final int index)
	{
		if (!columns.contains(column))
		{
			columns.add(index, column);
		}
	}

	public TableColumn remove(final int index)
	{
		return columns.remove(index);
	}

	public boolean remove(final TableColumn column)
	{
		return columns.remove(column);
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + (columns == null ? 0 : columns.hashCode());
		result = prime * result + (name == null ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj)
	{
		if (this == obj)
		{
			return true;
		}
		if (obj == null)
		{
			return false;
		}
		if (!(obj instanceof TableTemplate))
		{
			return false;
		}
		final TableTemplate other = (TableTemplate) obj;
		if (columns == null)
		{
			if (other.columns != null)
			{
				return false;
			}
		}
		else if (!columns.equals(other.columns))
		{
			return false;
		}
		if (name == null)
		{
			if (other.name != null)
			{
				return false;
			}
		}
		else if (!name.equals(other.name))
		{
			return false;
		}
		return true;
	}
}
